import { NMTopoBaseCommand } from '@app-cad/environments/nextmesh/types/command';
import { ReferencePlaneType, TopoDialogProps } from '../type';
import { useEffect, useRef, useState } from 'react';

import { i18n } from '@app-nextmesh/common/utils';

import CmdInteractor from './CmdInteractor';
import { SelectBasicSurfaceDialog } from '../cmd-common/selectBasicSurfaceDialog';

function CmdTorusCommand({ onClose, title }: TopoDialogProps) {
  const [formData, setFormData] = useState({
    planeType: ReferencePlaneType.XOY,
  });
  const cmdRef = useRef<CmdInteractor>(null);

  useEffect(() => {
    cmdRef.current = new CmdInteractor(formData.planeType);
    cmdRef.current.addExit(onClose);
    return () => {
      cmdRef.current?.destroy();
      cmdRef.current = null;
    };
  }, []);

  return (
    <>
      <SelectBasicSurfaceDialog
        title={title}
        onClose={onClose}
        formData={formData}
        setFormData={setFormData}
        cmdRef={cmdRef}
        placement='topLeft'></SelectBasicSurfaceDialog>
    </>
  );
}

export const cmdTorus: NMTopoBaseCommand = {
  id: 'cmdTorus',
  label: 'command.topo.torus',
  icon: 'commands/curved/point',
  create: (label: string, onClose: () => void) => (
    <CmdTorusCommand
      title={i18n.t(label)}
      onClose={onClose}
    />
  ),
};
